**************************
***	RUN USING STATA 16 ***
**************************

* Purpose: assign universal item numbers
* Last Updated: 06 May 2021
	
*********************************
*** ENGLISH                   ***
*********************************
	
	//loop through versions, prepare response data
	forvalues k = 1/18 {
		use "$input\eng_o_scored.dta", clear
		*retain only relevant form and vars
		keep if version==`k'
		keep pid version r1-r60
		order r*, seq
		order pid version
		*convert all to string, so to renvars from observation [1] later
		tostring r1-r60 pid version, replace force

	tempfile ver
	save `ver'	
	
	//lookup and setup unique variable names
	import excel "$input\eng key_domain.xlsx", sheet("location") firstrow clear

		*do for current form/version
		keep if form==`k'
		drop source ref_origin ref_form key type
		gen un = (src_yr-2010)*100 + src_loc
		replace src_typ = "o_" if src_typ=="obj"
		egen r = concat(src_typ un)
		drop src_yr src_loc src_typ un
		*reshape data to retain one observation with unique variable names
		reshape wide r, i(form) j(location)
		drop form
		gen version = "version"
		gen pid = "pid"
		
	//combine responses with universal references
	append using `ver'
	
		*rename all vars using universal reference
		renvars, map(strtoname(@[1]))
		drop if pid=="pid"
		order version, first
		order pid, last
		*destring all
		destring version - pid, replace force
		order version pid, first
		
	tempfile ver`k'
	save `ver`k''
	
	}
	
	//combine all forms/versions
	use `ver1'
		
		forvalues l = 2/18 {
		append using `ver`l''
		}
		
		*remove all variable labels
		foreach var of varlist _all {
		label var `var' ""
		}
	
	//save
	save "$output\eng_unique.dta", replace
	